<?php
error_reporting(7);

$templatelist = "articlehome,navbar_article,articlehome_pagejump,articlehome_pagejumpbit,articlehome_nextpage_subhead,navbar_article";
$templatelist .= ",articlehome_rate,articlehome_rated,articlehome_nextpage,articlehome_editor,articlehome_rate_unlogin,articlehome_comment,articlehome_commentbit,articlehome_editor,articlehome_commentbox";
$templatelist .= ",articlehome_relatedlinkbit,articlehome_relatedlink,articlehome_relatedarticlebit,articlehome_relatedarticle";

require "global.php";

if (empty($article)) {
        $article = validate_articleid($articleid);
} 
if (!$article[visible]) {
        show_errormessage("error_invalid_articleid");
} 

if (!$pauserinfo[isadmin] AND !$pauserinfo[canviewarticle]) {
        show_nopermission();
} 

$article[description] = htmlspecialchars(trim($article[description]));

$article[date] = padate($dateformat_article, $article[date]);

cachesorts();

$sortlist = makesortlist();
$navbar = makearticlenavbar($article);

if (!isset($pagenum) OR $pagenum < 1) {
        $pagenum = 1;
} 

$pages = $DB->query("SELECT id,subhead FROM " . $db_prefix . "articletext WHERE articleid='$articleid' ORDER BY displayorder,id");

$totalpages = $DB->num_rows($pages);

if ($pagenum > $totalpages) {
        $pagenum = $totalpages;
} 

$offset = $pagenum-1;

$DB->query("UPDATE " . $db_prefix . "article SET views=views+1 WHERE articleid='$articleid'");

$articletext = $DB->fetch_one_array("SELECT subhead,articletext
                                            FROM " . $db_prefix . "articletext
                                            WHERE articleid='$articleid'
                                            ORDER BY displayorder,id LIMIT $offset,1");

unset($pagejump);
if ($totalpages > 1) {
        $page = 1;
        unset($pagejumpbits);
        while ($pg = $DB->fetch_array($pages)) {
                $subhead[$page] = $pg[subhead];
                eval("\$pagejumpbits .= \"" . gettemplate('articlehome_pagejumpbit') . "\";");
                $page++;
        } 

        if ($pagenum >= $totalpages) {
                $nextpage = "";
        } else {
                $nextpagenum = $pagenum + 1;
                $nextsubhead = $subhead[$nextpagenum];
                eval("\$nextpage = \"" . gettemplate('articlehome_nextpage') . "\";");
        } 

        eval("\$pagejump = \"" . gettemplate('articlehome_pagejump') . "\";");
} 

$relatedlinks = $DB->query("SELECT * FROM " . $db_prefix . "relatedlink WHERE articleid='$articleid'");

if ($DB->num_rows($relatedlinks) > 0) {
        unset($relatedlinkbit);
        unset($relatedlink);
        while ($relatedlink = $DB->fetch_array($relatedlinks)) {
                eval("\$relatedlinkbit .= \"" . gettemplate('articlehome_relatedlinkbit') . "\";");
        } 
        eval("\$relatedlinkbits = \"" . gettemplate('articlehome_relatedlink') . "\";");
} 

if (pa_isset($article[keyword])) {
        $keywords = explode(",", $article[keyword]);
        $keywordcounter = count($keywords);
        unset($kc);
        for ($i = 0;$i < $keywordcounter;$i++) {
                $kc[] = "title LIKE '%" . addslashes(htmlspecialchars($keywords[$i])) . "%'";
        } 

        $kcsql = implode(" OR ", $kc);
        $relatedarticles = $DB->query("SELECT * FROM " . $db_prefix . "article WHERE
                                            ($kcsql) AND articleid!='$articleid'
                                            AND sortid IN (0" . getsubsorts($article[sortid]) . ")
                                            AND visible=1
                                            ORDER BY date DESC
                                            LIMIT 10");

        if ($DB->num_rows($relatedarticles) > 0) {
                unset($relatedarticlebit);
                unset($relatedarticle);
                while ($relatedarticle = $DB->fetch_array($relatedarticles)) {
                        $relatedarticle[date] = padate("m-d h:i a", $relatedarticle[date]);
                        eval("\$relatedarticlebit .= \"" . gettemplate('articlehome_relatedarticlebit') . "\";");
                } 
                eval("\$relatedarticlebits = \"" . gettemplate('articlehome_relatedarticle') . "\";");
        } 
} 

$votes = $DB->query("SELECT sum(vote) AS total,count(vote) as voters,vote
                             FROM " . $db_prefix . "articlerate
                             WHERE articleid='$articleid'
                             GROUP BY vote");
if ($DB->num_rows($votes) > 0) {
        while ($vote = $DB->fetch_array($votes)) {
                $voter[$vote[vote]] = $vote[voters];
                $scores[$vote[vote]] = $vote[total];
        } 
        $totalvoters = array_sum($voter);
        $totalscores = array_sum($scores);
} else {
        $totalvoters = 0;
        $totalscores = 0;
} 
$maxheight = 30;
for ($i = 1;$i <= 10;$i++) {
        if ($totalvoters == 0) {
                $barheight[$i] = 1;
        } else {
                $barheight[$i] = floor($voter[$i] / $totalvoters * $maxheight);
        } 
} 

if ($totalvoters > 0) {
        $average = number_format($totalscores / $totalvoters, 2, '.', '');
} else {
        $average = 0;
} 

unset($rate);
if ($pauserinfo[userid] != 0) {
        $checkvote = $DB->fetch_one_array("SELECT * FROM " . $db_prefix . "articlerate
                                                WHERE articleid='$articleid' AND userid='$pauserinfo[userid]'");
        if (empty($checkvote)) {
                if (!$pauserinfo[canratearticle] AND !$pauserinfo[isadmin]) {
                        eval("\$rate = \"" . gettemplate('articlehome_rate_nopermission') . "\";");
                } else {
                        eval("\$rate = \"" . gettemplate('articlehome_rate') . "\";");
                } 
        } else {
                $checkvote[date] = padate("Y-m-d H:i:s A", $checkvote[date]);
                eval("\$rate = \"" . gettemplate('articlehome_rated') . "\";");
        } 
} else {
        eval("\$rate = \"" . gettemplate('articlehome_rate_unlogin') . "\";");
} 

if (pa_isset($article[editor])) {
        eval("\$editor = \"" . gettemplate('articlehome_editor') . "\";");
} 

unset($commentbits);
if ($pauserinfo[canviewcomment] OR $pauserinfo[isadmin]) {
        $comments = $DB->query("SELECT * FROM " . $db_prefix . "comment
                                     WHERE articleid='$articleid'
                                     ORDER BY lastupdate DESC
                                     LIMIT 5");

        if ($DB->num_rows($comments) > 0) {
                unset($commentbit);
                while ($comment = $DB->fetch_array($comments)) {
                        $comment[date] = padate("Y-m-d H:i a", $comment[date]);
                        eval("\$commentbit .= \"" . gettemplate('articlehome_commentbit') . "\";");
                } 
                eval("\$commentbits = \"" . gettemplate('articlehome_comment') . "\";");
        } 
} 

unset($commentbox);
if ($pauserinfo[cancomment] OR $pauserinfo[isadmin]) {
        if ($pauserinfo[userid] != 0) {
                eval("\$commentbox = \"" . gettemplate('articlehome_commentbox') . "\";");
        } 
} 

eval("dooutput(\"" . gettemplate('articlehome') . "\");");

?>